using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;        // for "Directory", try/catch

namespace Tool
{
    public partial class Form1 : Form
    {
        //private System.Windows.Forms.TreeView TreeViewSample;      
        TreeNode RootNode;
        About aboutDlg = new About();

        public Form1()
        {
            // Required for Windows Form Designer support
            InitializeComponent();

            // Every TreeView requires a RootNode
            RootNode = new TreeNode();
            RootNode.Text = "My Computer";
            RootNode.Tag = "My Computer";

            // Add the root to the tree
            EventTree.Nodes.Add(RootNode);

            // Set root as selected node
            EventTree.SelectedNode = RootNode;

            // get this computer's drives
            getDrives(RootNode);

            // collapse all, to show only the drives
            EventTree.CollapseAll();

            //search the next layer below our currently selected node.
            EventTree.AfterExpand += new TreeViewEventHandler(TreeViewSample_AfterExpand);

            RootNode.Expand();
            RootNode.ContextMenuStrip = contextMenuStrip1;

            SoundBankGrid.Rows.Add(10);
            SoundBankGrid.Rows[0].Cells["Filename"].Value = "thisIsMyFavoriteSoundOfAllTimeSKEEEET";
            SoundBankGrid.Rows[0].Cells["FileType"].Value = "WAV";
            SoundBankGrid.Rows[0].Cells["Length"].Value = "00:00:13";
            SoundBankGrid.Rows[0].Cells["Frequency"].Value = "4400Hz";
            SoundBankGrid.Rows[0].Cells["BitDepth"].Value = "32-bit";
            SoundBankGrid.Rows[0].Cells["Channels"].Value = "69";

            this.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog.ShowDialog();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            aboutDlg.ShowDialog();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        // collects the drives on the system, adds them below the root node
        private void getDrives(TreeNode ParentNode)
        {
            // Ensures we have the root node set
            EventTree.SelectedNode = ParentNode;

            try
            {
                string[] drives = Directory.GetLogicalDrives();
                foreach (string drive in drives)
                {
                    // create a new node
                    TreeNode tempNode = new TreeNode();

                    // save the path
                    tempNode.Tag = drive.Substring(0, drive.Length);
                    tempNode.Text = drive.Substring(0, drive.Length);

                    // Add the node to the tree
                    EventTree.SelectedNode.Nodes.Add(tempNode);
                }
            }
            catch (IOException e)
            {
                MessageBox.Show(e.Message.ToString());
            }
        }


        // collects directory names one level below the current directory node
        private void getNextDirectories(TreeNode ParentNode)
        {
            // ensure we're on the right node
            EventTree.SelectedNode = ParentNode;

            // we only need to get directories if it hasn't been done yet
            if (ParentNode.Nodes.Count == 0)
            {
                // set path for directory search
                string path = ParentNode.Tag.ToString() + "\\";

                try
                {
                    foreach (string directory in Directory.GetDirectories(path))
                    {
                        TreeNode tempNode = new TreeNode();
                        tempNode.Tag = directory;
                        tempNode.Text = directory.Substring(directory.LastIndexOf("\\") + 1);

                        EventTree.SelectedNode.Nodes.Add(tempNode);
                    }
                }
                catch( Exception e )
                {
                    string msg = e.Message.ToString();
                }
            }

        }


        // creates the (+) next to directories with subdirectories, 
        // by adding nodes of directories that are one level below the current location
        private void TreeViewSample_AfterExpand(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            // set first directory below current location
            EventTree.SelectedNode = e.Node.FirstNode;

            while (EventTree.SelectedNode != null)
            {
                // get the subdirectories
                getNextDirectories(EventTree.SelectedNode);

                // get the next node
                EventTree.SelectedNode = EventTree.SelectedNode.NextNode;
            }

            // reset the selected node to the node we started with
            EventTree.SelectedNode = e.Node;
        }

        // when we click our mouse on a location we can locate the node/directory the user clicked on
        private void TreeViewSample_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            TreeNode tempNode = EventTree.GetNodeAt(e.X, e.Y);

            // set our current node to where the user clicked
            EventTree.SelectedNode = tempNode;
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void treeView1_AfterSelect_1(object sender, TreeViewEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }
    }
}